/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package vistas;

import entidades.CambioDeRepuestos;
import entidades.CambioRepuestosLote;
import entidades.IngresoRepuesto;
import entidades.Repuesto;
import entidades.RuedasReemplazadas;
import entidades.Tecnico;
import entidades.Vehiculo;
import java.awt.Color;
import java.awt.Point;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import reportes.GenReportes;
import rn.RepuestoRN;
import rn.VehiculoRN;

/**
 *
 * @author g
 */
public class FormCambioDeRepuestos extends javax.swing.JFrame {

    public static Vehiculo vehiculo_selected;
    public List<CambioDeRepuestos> ls_cambios = new ArrayList<>();
    DefaultTableModel modelo_tabla;

    public FormCambioDeRepuestos() {
        initComponents();
        iniciarModeloTabla();
        cargarTecnicos();
        this.setLocationRelativeTo(null);
        this.setVisible(true);
        tf_vehiculo_legajo.addKeyListener(new buscarVehiculo());
        bt_confirmar_vehiculo.addKeyListener(new confirmarVehiculo());
        bt_agregar.addKeyListener(new AgregarRepuesto());
        habilitarFichaTecnica(false);
        bt_guardar.setEnabled(false);
        tf_vehiculo_legajo.requestFocus();
        
        //Para accionar con doble clic
        tabla.addMouseListener(new MouseAdapter() {
            public void mousePressed(MouseEvent me) {
                JTable table = (JTable) me.getSource();
                Point p = me.getPoint();
                int row = table.rowAtPoint(p);                
                if(me.getClickCount() == 1){
                    tabla.changeSelection(row, 0, false, false);
                    bt_quitar.setEnabled(true);                                        
                }
            }
        });
        
    }

    private void cargarTecnicos() {
        combo_tecnico.removeAllItems();
        List<Tecnico> todosTecnicos = RepuestoRN.getTodosTecnicos();
        for (Tecnico t : todosTecnicos) {
            combo_tecnico.addItem(t);
        }
    }

    public class buscarVehiculo extends KeyAdapter {

        public void keyPressed(KeyEvent e) {
            tf_vehiculo_legajo.setBackground(Color.WHITE);
            tf_info.setText(null);
            if (e.getSource() == tf_vehiculo_legajo) {
                if (e.VK_ENTER == e.getKeyCode()) {
                    String id_text = tf_vehiculo_legajo.getText();
                    if (verificarValor(id_text)) {
                        Vehiculo v = VehiculoRN.findVehiculoPorID(new Long(id_text));
                        if (v != null) {
                            vehiculo_selected = v;
                            tf_vehiculo_descripcion.setText(v.getDescripcion());
                            tf_vehiculo_descripcion.setBackground(new Color(204, 255, 255));
                            tf_vehiculo_marca.setText(v.getModelo().getMarca().getDescripcion());
                            tf_vehiculo_marca.setBackground(new Color(204, 255, 255));
                            tf_vehiculo_modelo.setText(v.getModelo().getDescripcion());
                            tf_vehiculo_modelo.setBackground(new Color(204, 255, 255));
                            bt_confirmar_vehiculo.setEnabled(true);
                            bt_confirmar_vehiculo.requestFocus();
                        } else {
                            tf_info.setText("No se encuentra vehículo");
                            tf_vehiculo_descripcion.setText(null);
                            tf_vehiculo_marca.setText(null);
                            tf_vehiculo_modelo.setText(null);
                        }
                    } else {
                        tf_vehiculo_legajo.setBackground(Color.PINK);
                        tf_info.setText("el código ingresado es invalido o no se encuentra vehículo");
                        tf_vehiculo_descripcion.setText(null);
                        tf_vehiculo_marca.setText(null);
                        tf_vehiculo_modelo.setText(null);
                    }
                }
                if (e.VK_F2 == e.getKeyCode()) {
                    new FormSeleccionarVehiculo(2);
                }
            }
        }
    }    

    public class confirmarVehiculo extends KeyAdapter {

        @Override
        public void keyPressed(KeyEvent e) {
            if (e.VK_S == e.getKeyCode() || e.VK_ENTER == e.getKeyCode()) {
                tf_vehiculo_legajo.setEditable(false);
                bt_buscar_vehiculo.setEnabled(false);
                bt_agregar.setEnabled(true);
                bt_quitar.setEnabled(true);
                bt_agregar.requestFocus();
                bt_confirmar_vehiculo.setEnabled(false);
                habilitarFichaTecnica(true);
                bt_guardar.setEnabled(true);
            }
            if (e.VK_N == e.getKeyCode()) {
                tf_vehiculo_legajo.requestFocus();
            }
        }
    }

    private class AgregarRepuesto extends KeyAdapter {

        public void keyPressed(KeyEvent e) {
            if (e.VK_ENTER == e.getKeyCode() && e.getSource() == bt_agregar) {
                seleccionarRepuesto();
                e.consume();
            }
        }
    }

    private void seleccionarRepuesto() {
        if (vehiculo_selected != null) {
            new SeleccionarRepuesto(this);
        } else {
            JOptionPane.showMessageDialog(null, "Debe selecionar un vehículo");
        }
    }

    private boolean verificarValor(String text) {
        return (text.matches("[+]?\\d*(\\\\d+)?") && text.equals("") == false);
    }

    public void agregarRepuestoALista(Repuesto r, int eje, int rueda) {
        CambioDeRepuestos cambio = new CambioDeRepuestos();
        cambio.setRepuesto(r);
        cambio.setVehiculo(vehiculo_selected);
        if (eje != 0 && rueda != 0) {
            RuedasReemplazadas ru = new RuedasReemplazadas();
            ru.setNro_eje(eje);
            ru.setNro_rueda(rueda);
            ru.setId_repuesto_cambiado(r.getId());
            cambio.setRueda(true);
            cambio.setRuedasReemplazadas(ru);
        }
        ls_cambios.add(cambio);
        agregarATabla(r, eje, rueda);
    }

    private void agregarATabla(Repuesto r, int eje, int rueda) {
        Object fila[];
        if (eje != 0 && rueda != 0) {
            fila = new Object[]{
                r.getDescripcion(), r.getNro_parte(), r.getModeloRepuesto().getDescripcion(), r.getModeloRepuesto().getMarca().getDescripcion(), "Eje: " + eje + "- Rueda: " + rueda
            };
        } else {
            fila = new Object[]{
                //r.getDescripcion(), r.getNro_parte(), r.getModeloRepuesto().getDescripcion(), r.getModeloRepuesto().getMarca().getDescripcion(), "Sin Ref."
                r.getDescripcion(), r.getNro_parte(), r.getModeloRepuesto().getDescripcion(), r.getModeloRepuesto().getMarca().getDescripcion()
            };
        }

        modelo_tabla.addRow(fila);
        tabla.setModel(modelo_tabla);
    }

    private void iniciarModeloTabla() {
        Object datos[][] = {};
        //String[] columnNames = {"Descripcion", "Nro. parte", "Modelo", "Descripción", "Referencias"};
        String[] columnNames = {"Descripcion", "Nro. parte", "Modelo", "Descripción"};
        modelo_tabla = new DefaultTableModel(datos, columnNames);
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel6 = new javax.swing.JLabel();
        tf_vehiculo_descripcion = new javax.swing.JTextField();
        tf_vehiculo_legajo = new javax.swing.JTextField();
        bt_buscar_vehiculo = new javax.swing.JButton();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        tf_vehiculo_marca = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();
        tf_vehiculo_modelo = new javax.swing.JTextField();
        bt_confirmar_vehiculo = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tabla = new javax.swing.JTable();
        bt_agregar = new javax.swing.JButton();
        bt_quitar = new javax.swing.JButton();
        jPanel3 = new javax.swing.JPanel();
        jLabel12 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        area_obs = new javax.swing.JTextArea();
        jLabel10 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        combo_calendario = new org.freixas.jcalendar.JCalendarCombo();
        combo_tecnico = new javax.swing.JComboBox();
        bt_agregar_tecnico = new javax.swing.JButton();
        bt_cancelar = new javax.swing.JButton();
        bt_guardar = new javax.swing.JButton();
        jPanel4 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        tf_info = new javax.swing.JLabel();
        bt_salir = new javax.swing.JButton();
        jPanel5 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Datos del vehiculo"));

        jLabel6.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel6.setText("Descripción:");

        tf_vehiculo_descripcion.setEditable(false);

        tf_vehiculo_legajo.setBackground(new java.awt.Color(204, 255, 255));
        tf_vehiculo_legajo.setToolTipText("F2 - BUSCAR");
        tf_vehiculo_legajo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tf_vehiculo_legajoActionPerformed(evt);
            }
        });

        bt_buscar_vehiculo.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        bt_buscar_vehiculo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/arrow_left.png"))); // NOI18N
        bt_buscar_vehiculo.setText("F2 - Buscar vehículo");
        bt_buscar_vehiculo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_buscar_vehiculoActionPerformed(evt);
            }
        });

        jLabel7.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel7.setText("Legajo:");

        jLabel8.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel8.setText("Marca:");

        tf_vehiculo_marca.setEditable(false);

        jLabel9.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel9.setText("Modelo:");

        tf_vehiculo_modelo.setEditable(false);

        bt_confirmar_vehiculo.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        bt_confirmar_vehiculo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/help.png"))); // NOI18N
        bt_confirmar_vehiculo.setText("Confirmar? SI 'S'  - NO 'N'");
        bt_confirmar_vehiculo.setEnabled(false);
        bt_confirmar_vehiculo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_confirmar_vehiculoActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jLabel6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jLabel7, javax.swing.GroupLayout.DEFAULT_SIZE, 84, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(tf_vehiculo_legajo, javax.swing.GroupLayout.DEFAULT_SIZE, 191, Short.MAX_VALUE)
                            .addComponent(bt_buscar_vehiculo, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGap(18, 18, 18)
                                .addComponent(jLabel8, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(tf_vehiculo_marca, javax.swing.GroupLayout.PREFERRED_SIZE, 191, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(24, 24, 24)
                                .addComponent(jLabel9, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(tf_vehiculo_modelo, javax.swing.GroupLayout.PREFERRED_SIZE, 191, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGap(31, 31, 31)
                                .addComponent(bt_confirmar_vehiculo, javax.swing.GroupLayout.PREFERRED_SIZE, 207, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(0, 0, Short.MAX_VALUE))))
                    .addComponent(tf_vehiculo_descripcion))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel8, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(tf_vehiculo_marca, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel9, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(tf_vehiculo_modelo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(tf_vehiculo_legajo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(tf_vehiculo_descripcion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(bt_buscar_vehiculo)
                    .addComponent(bt_confirmar_vehiculo))
                .addContainerGap())
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Lista de repuestos a cambiar"));

        tabla.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        tabla.setEnabled(false);
        jScrollPane1.setViewportView(tabla);

        bt_agregar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/add.png"))); // NOI18N
        bt_agregar.setText("Agregar");
        bt_agregar.setEnabled(false);
        bt_agregar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_agregarActionPerformed(evt);
            }
        });

        bt_quitar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/cancel.png"))); // NOI18N
        bt_quitar.setText("Quitar");
        bt_quitar.setEnabled(false);
        bt_quitar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_quitarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 700, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(bt_agregar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(bt_quitar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(0, 0, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(bt_agregar, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(bt_quitar, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 94, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Ficha técnica"));

        jLabel12.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel12.setText("Fecha de cambio:");

        area_obs.setColumns(20);
        area_obs.setRows(5);
        jScrollPane2.setViewportView(area_obs);

        jLabel10.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel10.setText("Técnico:");

        jLabel11.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel11.setText("Observaciones:");

        bt_agregar_tecnico.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/add.png"))); // NOI18N
        bt_agregar_tecnico.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_agregar_tecnicoActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel11, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 456, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jLabel10, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel12)
                    .addComponent(combo_tecnico, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(combo_calendario, javax.swing.GroupLayout.PREFERRED_SIZE, 292, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(bt_agregar_tecnico, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(jLabel10, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(combo_tecnico, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(bt_agregar_tecnico, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jLabel12, javax.swing.GroupLayout.PREFERRED_SIZE, 18, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(combo_calendario, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(jLabel11, javax.swing.GroupLayout.PREFERRED_SIZE, 18, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(13, 13, 13)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        bt_cancelar.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        bt_cancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/delete.png"))); // NOI18N
        bt_cancelar.setText("CANCELAR");
        bt_cancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_cancelarActionPerformed(evt);
            }
        });

        bt_guardar.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        bt_guardar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/accept.png"))); // NOI18N
        bt_guardar.setText("GUARDAR");
        bt_guardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_guardarActionPerformed(evt);
            }
        });

        jPanel4.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jLabel1.setText("Info:");

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(tf_info, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addComponent(tf_info, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        bt_salir.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        bt_salir.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/cancel.png"))); // NOI18N
        bt_salir.setText("SALIR");
        bt_salir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_salirActionPerformed(evt);
            }
        });

        jPanel5.setBackground(new java.awt.Color(0, 0, 153));

        jLabel2.setBackground(new java.awt.Color(255, 255, 255));
        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
        jLabel2.setForeground(new java.awt.Color(255, 255, 255));
        jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel2.setText("CAMBIO DE REPUESTOS");

        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel5Layout.createSequentialGroup()
                .addGap(0, 0, Short.MAX_VALUE)
                .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 865, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel5, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addGap(520, 520, 520)
                                .addComponent(bt_guardar)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(bt_cancelar)
                                .addGap(3, 3, 3)
                                .addComponent(bt_salir)))
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(bt_cancelar, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(bt_guardar, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(bt_salir, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void bt_buscar_vehiculoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_buscar_vehiculoActionPerformed
        FormSeleccionarVehiculo form = new FormSeleccionarVehiculo(2);
    }//GEN-LAST:event_bt_buscar_vehiculoActionPerformed

    private void bt_quitarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_quitarActionPerformed
        if (tabla.getSelectedRow() != -1) {
            int selectedRow = tabla.getSelectedRow();
            ls_cambios.remove(selectedRow);
            modelo_tabla.removeRow(selectedRow);
            tabla.setModel(modelo_tabla);
        }
    }//GEN-LAST:event_bt_quitarActionPerformed

    private void bt_agregarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_agregarActionPerformed
        seleccionarRepuesto();
    }//GEN-LAST:event_bt_agregarActionPerformed

    private void bt_guardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_guardarActionPerformed
        if (vehiculo_selected != null) {
            if (!ls_cambios.isEmpty()) {
                for (CambioDeRepuestos c : ls_cambios) {
                    if (c.isRueda()) {
                        RepuestoRN.create(c.getRuedasReemplazadas());
                    }
                    RepuestoRN.create(c);
                    //Ahora disminuir stock del repuesto
                    c.getRepuesto().setStock(c.getRepuesto().getStock() - 1);
                    RepuestoRN.edit(c.getRepuesto());
                }

                CambioRepuestosLote lote = new CambioRepuestosLote();
                lote.setEncargado(Principal.usuario);
                lote.setFecha(combo_calendario.getDate());
                lote.setTecnico((Tecnico) combo_tecnico.getSelectedItem());
                lote.setObservaciones(area_obs.getText());
                lote.setVehiculo(vehiculo_selected);
                lote.setLs_cambios(ls_cambios);
                RepuestoRN.create(lote);

                JOptionPane.showMessageDialog(null, "Datos Guardados!!!");
                limpiarFormulario();
                GenReportes r = new GenReportes();
                r.repCambioDeRepuestoXLote(RepuestoRN.getUltimoLoteDeCambio());

                bt_agregar.setEnabled(false);
                bt_quitar.setEnabled(false);
                bt_confirmar_vehiculo.setEnabled(false);
                bt_buscar_vehiculo.setEnabled(true);
                tf_vehiculo_legajo.setEditable(true);
                limpiarFormulario();
                tf_vehiculo_legajo.requestFocus();

            } else {
                JOptionPane.showMessageDialog(null, "La tabla 'Repuestos a Cambiar' No puede estar vacía");
            }
        } else {
            JOptionPane.showMessageDialog(null, "Debe selecionar un vehículo");
        }


    }//GEN-LAST:event_bt_guardarActionPerformed

    private void bt_cancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_cancelarActionPerformed

        int op = JOptionPane.showConfirmDialog(null, "¿Desea cancelar TODO el ingreso?");
        if (op == 0) {
            bt_agregar.setEnabled(false);
            bt_quitar.setEnabled(false);
            bt_confirmar_vehiculo.setEnabled(false);
            bt_buscar_vehiculo.setEnabled(true);
            tf_vehiculo_legajo.setEditable(true);
            limpiarFormulario();
            tf_vehiculo_legajo.requestFocus();
        }

    }//GEN-LAST:event_bt_cancelarActionPerformed

    private void tf_vehiculo_legajoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tf_vehiculo_legajoActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_tf_vehiculo_legajoActionPerformed

    private void bt_salirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_salirActionPerformed
        this.dispose();
    }//GEN-LAST:event_bt_salirActionPerformed

    private void bt_confirmar_vehiculoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_confirmar_vehiculoActionPerformed
        tf_vehiculo_legajo.setEditable(false);
        bt_buscar_vehiculo.setEnabled(false);
        bt_agregar.setEnabled(true);
        bt_quitar.setEnabled(true);
        bt_agregar.requestFocus();
        bt_confirmar_vehiculo.setEnabled(false);
        habilitarFichaTecnica(true);
        bt_guardar.setEnabled(true);
    }//GEN-LAST:event_bt_confirmar_vehiculoActionPerformed

    private void bt_agregar_tecnicoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_agregar_tecnicoActionPerformed
        ABM_Tecnico abmt = new ABM_Tecnico();
        cargarTecnicos();
    }//GEN-LAST:event_bt_agregar_tecnicoActionPerformed

    public List<CambioDeRepuestos> getLs_cambios() {
        return ls_cambios;
    }

    public void setLs_cambios(List<CambioDeRepuestos> ls_cambios) {
        this.ls_cambios = ls_cambios;
    }

    private void habilitarFichaTecnica(boolean b) {
        area_obs.setEnabled(b);
        combo_calendario.setEnabled(b);
        combo_tecnico.setEnabled(b);
        bt_agregar_tecnico.setEnabled(b);
    }


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextArea area_obs;
    private javax.swing.JButton bt_agregar;
    private javax.swing.JButton bt_agregar_tecnico;
    private javax.swing.JButton bt_buscar_vehiculo;
    private javax.swing.JButton bt_cancelar;
    public static javax.swing.JButton bt_confirmar_vehiculo;
    private javax.swing.JButton bt_guardar;
    private javax.swing.JButton bt_quitar;
    private javax.swing.JButton bt_salir;
    private org.freixas.jcalendar.JCalendarCombo combo_calendario;
    private javax.swing.JComboBox combo_tecnico;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable tabla;
    private javax.swing.JLabel tf_info;
    public static javax.swing.JTextField tf_vehiculo_descripcion;
    public static javax.swing.JTextField tf_vehiculo_legajo;
    public static javax.swing.JTextField tf_vehiculo_marca;
    public static javax.swing.JTextField tf_vehiculo_modelo;
    // End of variables declaration//GEN-END:variables

    private void limpiarFormulario() {
        modelo_tabla = new DefaultTableModel();
        tabla.setModel(modelo_tabla);
        iniciarModeloTabla();
        vehiculo_selected = null;
        tf_vehiculo_descripcion.setText(null);
        tf_vehiculo_descripcion.setBackground(Color.WHITE);
        tf_vehiculo_legajo.setText(null);
        tf_vehiculo_legajo.setBackground(Color.WHITE);
        tf_vehiculo_marca.setText(null);
        tf_vehiculo_marca.setBackground(Color.WHITE);
        tf_vehiculo_modelo.setText(null);
        tf_vehiculo_modelo.setBackground(Color.WHITE);
        area_obs.setText(null);
        ls_cambios = new ArrayList<>();
    }
}
